package cn.schoolwow.quickdao.module.database.ddl.query.flow.property.getProperty;

import cn.schoolwow.quickdao.annotation.IdStrategy;
import cn.schoolwow.quickdao.domain.entity.Property;
import cn.schoolwow.quickdao.module.database.parent.domain.ResultSetConsumer;
import cn.schoolwow.quickdao.module.database.parent.flow.executor.ExecuteQueryConnectionFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.sql.ResultSet;
import java.util.Arrays;

public class GetPropertyBySQLServerFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        String tableName = (String) flowContext.checkData("tableName");
        String columnName = (String) flowContext.checkData("columnName");
        flowContext.startFlow(new ExecuteQueryConnectionFlow())
                .putTemporaryData("name", "获取指定表字段类型信息")
                .putTemporaryData("sql", "select ordinal_position,column_type,is_nullable from information_schema.columns where table_name = ? and column_name = ?;")
                .putTemporaryData("parameters", Arrays.asList(tableName, columnName))
                .putReturnData("resultSetConsumer",new ResultSetConsumer() {
                    @Override
                    public void consumeResultSet(ResultSet resultSet) throws Exception {
                        if(resultSet.next()){
                            Property property = new Property();
                            property.column = resultSet.getString("column_name");
                            property.columnType = resultSet.getString("column_type");
                            property.notNull = "NO".equals(resultSet.getString("is_nullable"));
                            flowContext.putData("property", property);
                        }
                    }
                })
                .execute();

        if(flowContext.containKey("property")){
            flowContext.broken("字段不存在");
        }
        Property property = (Property) flowContext.checkData("property");

        flowContext.startFlow(new ExecuteQueryConnectionFlow())
                .putTemporaryData("name", "获取指定表字段注释")
                .putTemporaryData("sql", "select convert(varchar(255),a.value) value from sys.extended_properties a, sysobjects b, sys.columns c where a.major_id = b.id and c.object_id = b.id and c.column_id = a.minor_id and b.name = ? and c.name = ?;")
                .putTemporaryData("parameters", Arrays.asList(tableName, columnName))
                .putReturnData("resultSetConsumer",new ResultSetConsumer() {
                    @Override
                    public void consumeResultSet(ResultSet resultSet) throws Exception {
                        if(resultSet.next()){
                            property.comment = resultSet.getString("value");
                        }
                    }
                })
                .execute();

        flowContext.startFlow(new ExecuteQueryConnectionFlow())
                .putTemporaryData("name", "获取指定表主键")
                .putTemporaryData("sql", "select column_name from information_schema.key_column_usage where table_name = ? and column_name = ?")
                .putTemporaryData("parameters", Arrays.asList(tableName, columnName))
                .putReturnData("resultSetConsumer",new ResultSetConsumer() {
                    @Override
                    public void consumeResultSet(ResultSet resultSet) throws Exception {
                        if(resultSet.next()){
                            property.id = true;
                            property.strategy = IdStrategy.AutoIncrement;
                        }
                    }
                })
                .execute();
    }

    @Override
    public String name() {
        return "获取数据库字段-SQLServer数据库";
    }
}
